package 继承覆盖综合练习;

import java.util.*;


//创建若干Student对象、Employee对象。
//输入s，然后依次输入name age gender stuNo clazz创建Student对象。
//输入e，然后依次输入name age gender salary company创建Employee对象。
//然后将创建好的对象放入List<Person> personList。输入其他字符，则结束创建。
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        List<Person> personList=new ArrayList<>();
        int n=0;
        while (true)
        {
            String key=sc.next();
            if(key.equals("s"))
            {
                personList.add(new Student(sc.next(),sc.nextInt(),sc.nextBoolean(),sc.next(),sc.next()));
            }
            else if ((key.equals("e")))
            {
                personList.add(new Employee(sc.next(),sc.nextInt(),sc.nextBoolean(),sc.nextDouble(),new Company(sc.next())));
            }
            else
                break;
            personList.get(n++).n=key;
        }
        Collections.sort(personList);
        personList.sort((obj1, obj2) -> {
            if (obj1.getName().compareTo(obj2.getName()) < 0) {
                return -1;
            } else if (obj1.getName().compareTo(obj2.getName()) > 0) {
                return 1;
            } else {
                return obj1.getAge() - obj2.getAge();
            }
        });
        for (int i=0;i<personList.size();++i)
        {
            System.out.println(personList.get(i).toString());
        }
        if(sc.next().equals("exit"))
        {
            return;
        }
        //将personList中的元素按照类型分别放到stuList与empList。
        // 注意：不要将两个内容相同的对象放入列表（是否相同是根据equals返回结果进行判定）。
        LinkedHashSet<Student> stuList = new LinkedHashSet<>();
        LinkedHashSet<Employee> empList = new LinkedHashSet<>();
        for (int i=0;i<personList.size();++i)
        {
            if(personList.get(i).n.equals("s"))
            {
                stuList.add(((Student) personList.get(i)));
            }
            else
            {
                empList.add(((Employee) personList.get(i)));
            }
        }
        System.out.println("stuList");
        for (Student i:stuList
             ) {
            System.out.println(i.toString());
        }
        System.out.println("empList");
        for (Employee i:empList
             ) {
            System.out.println(i.toString());
        }
    }

}
//public Person(String name, int age, boolean gender);
//public String toString();         //返回"name-age-gender"格式的字符串
//public boolean equals(Object obj);//比较name、age、gender,都相同返回true,否则返回false
 class Person implements Comparable
{
    private String name;
    private int age;
    private boolean gender;
    String n;
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public boolean isGender() {
        return gender;
    }

    public void setGender(boolean gender) {
        this.gender = gender;
    }

    public Person(String name, int age, boolean gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    Person() {
    }
    public String toString()
    {
        return name+"-"+String.valueOf(age)+"-"+String.valueOf(gender);
    }
    public boolean equals(Object obj)
    {
        Person n=(Person) obj;
        if(this.name.equals(n.getName())&&String.valueOf(this.age).equals(String.valueOf(n.getAge()))&&String.valueOf(this.gender).equals(String.valueOf(n.isGender())))
            return true;
        else
            return false;
    }

    @Override
    public int compareTo(Object o) {
        Person n=(Person) o;

        if(name.equals(n.getName()))
        {
            return age-n.getAge();
        }
        else
        {
            return name.compareTo(n.getName());
        }
    }

    @Override
    public int hashCode() {
        return this.name.hashCode();
    }
}
//Student类继承自Person，属性:String stuNo, String clazz
//建议使用super复用Person类的相关有参构造函数
//public Student(String name, int age, boolean gender, String stuNo, String clazz);
//public String toString();         //返回 “Student:person的toString-stuNo-clazz”格式的字符串
//public boolean equals(Object obj);//首先调用父类的equals方法,如果返回true，则继续比较stuNo与clazz。
class Student extends Person
{
    private String stuNo;
    private String clazz;
    @Override
    public String toString() {
        return "Student:"+super.toString()+"-"+stuNo+"-"+clazz;
    }

    @Override
    public boolean equals(Object obj) {
       if(super.equals(obj))
       {
           Student n=(Student) obj;
           if(stuNo==null||clazz==null||n.getStuNo()==null||n.getClazz()==null)
               return false;
           else
           {
               if (stuNo.equals(n.getStuNo()) && clazz.equals(n.getClazz()))
                   return true;
               else
                   return false;
           }
       }
       else
           return false;

    }

    public String getStuNo() {
        return stuNo;
    }

    public void setStuNo(String stuNo) {
        this.stuNo = stuNo;
    }

    public String getClazz() {
        return clazz;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }

    public Student(String name, int age, boolean gender, String stuNo, String clazz) {
        super(name, age, gender);
        this.stuNo = stuNo;
        this.clazz = clazz;

    }

}
//Company类属性：String name
//public Company(String name);
//public String toString();         //直接返回name
//public boolean equals(Object obj);//name相同返回true
 class Company
{
    private String name;

    public Company(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }

    @Override
    public boolean equals(Object obj) {
        Company n=(Company) obj;
        if(name.equals(n.getName()))
            return true;
        else
            return false;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
//Employee类继承自Person，属性：Company company, double salary
////建议使用super复用Person类的相关有参构造函数
//public Employee(String name, int age, boolean gender, double salary, Company company);
//public String toString();         //返回"Employee:person的toString-company-salary"格式的字符串
//public boolean equals(Object obj);//首先调用父类的equals方法,如果返回true。再比较company与salary。
////比较salary属性时，使用DecimalFormat df = new DecimalFormat("#.#");保留1位小数
//编写equals方法重要说明：
//
//对Employee的company属性的比较。要考虑传入为null的情况。如果company不为null且传入为null，返回false
//对所有String字符类型比较时，也要考虑null情况。
 class Employee extends Person
{
    private Company company;
    private double salary;

    public Employee(String name, int age, boolean gender, double salary, Company company ) {
        super(name, age, gender);
        this.company = company;
        this.salary = salary;
        if(company.toString().equals("null"))
        {
            this.company=null;
        }
    }

    @Override
    public String toString() {
        if(company!=null)
        return "Employee:"+super.toString()+"-"+company.toString()+"-"+String.valueOf(salary);
        else
            return "Employee:"+super.toString()+"-null-"+String.valueOf(salary);
    }

    @Override
    public boolean equals(Object obj) {
        Employee n=(Employee) obj;
        if(super.equals(n))
        {
            if(company==null&&n.getCompany()!=null||company!=null&&n.getCompany()==null)
                return false;
            else if(company==null&&n.getCompany()==null)
            {
                String str1=String.format("%.1f",salary);
                String str2=String.format("%.1f",n.getSalary());
                if(str1.equals(str2))
                    return true;
                else
                    return false;
            }
            else
            {
                String str1=String.format("%.1f",salary);
                String str2=String.format("%.1f",n.getSalary());
                if(str1.equals(str2)&&company.toString().equals(n.getCompany().toString()))
                    return true;
                else
                    return false;
            }

        }
        else
            return false;

    }

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
}